


/*
'===============================================================================
'  TuanHuy.BL.CategoryManagerBase
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using TuanHuy.BLL;
using TuanHuy.DAL;
using TuanHuy.DAL.EntityClasses;
using TuanHuy.DAL.FactoryClasses;
using TuanHuy.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace TuanHuy.BLL
{
	public class CategoryManagerBase : ManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		
		public CategoryManagerBase()
		{
			// Nothing for now.
		}
		
		/*public override string TableName()
        {
            return "Category";
        }*/
		
		public CategoryEntity Insert(CategoryEntity _CategoryEntity)
		{
			return Insert(_CategoryEntity, "");
		}
		
		public CategoryEntity Insert(CategoryEntity _CategoryEntity, string controlId)
		{
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.SaveEntity(_CategoryEntity, true);
				CategoryFields field = new CategoryFields();
                //LogEntity(_CategoryEntity, field, AcInsert, controlId);
			}
			return _CategoryEntity;
		}

		
		public CategoryEntity Insert(int CategoryId, string CategoryName)
		{
			CategoryEntity _CategoryEntity = new CategoryEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_CategoryEntity.CategoryId = CategoryId;
				_CategoryEntity.CategoryName = CategoryName;
				CategoryFields field = new CategoryFields();
                //LogEntity(_CategoryEntity, field, AcInsert);
				adapter.SaveEntity(_CategoryEntity, true);
			}
			return _CategoryEntity;
		}

		/*public CategoryEntity Insert(string CategoryName)
		{
			CategoryEntity _CategoryEntity = new CategoryEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_CategoryEntity.CategoryName = CategoryName;
				adapter.SaveEntity(_CategoryEntity, true);
				CategoryFields field = new CategoryFields();
                //LogEntity(_CategoryEntity, field, AcInsert);
			}
			return _CategoryEntity;
		}*/
		
		public bool Update(CategoryEntity _CategoryEntity)
        {
            return Update(_CategoryEntity, "");
        }

		public bool Update(CategoryEntity _CategoryEntity, string controlId)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				RelationPredicateBucket filter = new RelationPredicateBucket();
				IPredicateExpression _PredicateExpression = new PredicateExpression();
				_PredicateExpression.Add(CategoryFields.CategoryId == _CategoryEntity.CategoryId);
					
				filter.PredicateExpression.Add(_PredicateExpression);

				adapter.UpdateEntitiesDirectly(_CategoryEntity, filter);
				toReturn = true;
				
				CategoryFields field = new CategoryFields();
                //LogEntity(_CategoryEntity, field, AcUpdate, controlId);
			}
			return toReturn;
		}
		
		public bool Update(CategoryEntity _CategoryEntity, RelationPredicateBucket filter)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.UpdateEntitiesDirectly(_CategoryEntity, filter);
				toReturn = true;
				
				CategoryFields field = new CategoryFields();
                //LogEntity(_CategoryEntity, field, AcUpdate);
			}
			return toReturn;
		}

		public bool Update(int CategoryId, string CategoryName)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				CategoryEntity _CategoryEntity = new CategoryEntity(CategoryId);
				if (adapter.FetchEntity(_CategoryEntity))
				{
				
					_CategoryEntity.CategoryName = CategoryName;
					adapter.SaveEntity(_CategoryEntity, true);
					toReturn = true;
					
					CategoryFields field = new CategoryFields();
					//LogEntity(_CategoryEntity, field, AcUpdate);
				}
			}
			return toReturn;
		}

		public bool Delete(int CategoryId)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				CategoryEntity _CategoryEntity = new CategoryEntity(CategoryId);
				if (adapter.FetchEntity(_CategoryEntity))
				{
					adapter.DeleteEntity(_CategoryEntity);
					toReturn = true;
				}
			}
			return toReturn;
		}
		
		public void DeleteAll() 
		{
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.DeleteEntitiesDirectly("CategoryEntity", null);
			}
		}
		
		
		public int DeleteByCategoryId(int CategoryId)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryId == CategoryId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("CategoryEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByCategoryName(string CategoryName)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryName == CategoryName);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("CategoryEntity", filter);
			}
			return toReturn;
		}
		

		
		public CategoryEntity SelectOne(int CategoryId)
		{
			CategoryEntity toReturn = null;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				CategoryEntity _CategoryEntity = new CategoryEntity(CategoryId);
				if (adapter.FetchEntity(_CategoryEntity))
				{
					toReturn = _CategoryEntity;
				}
			}
			return toReturn;
		}

		// Return DataTable
		public DataTable SelectAllRDT()
		{
			DataTable toReturn = new DataTable();
			EntityCollection _CategoryCollection = new EntityCollection(new CategoryEntityFactory());
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_CategoryCollection.EntityFactoryToUse.CreateFields(), toReturn, null, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection<CategoryEntity>
		public EntityCollection<CategoryEntity> SelectAllLST()
		{
			EntityCollection<CategoryEntity> _CategoryCollection = new EntityCollection<CategoryEntity>();
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_CategoryCollection, null, 0, null);
			}
			return _CategoryCollection;
		}
		
		// Return EntityCollection<CategoryEntity>
		public EntityCollection<CategoryEntity> SelectAllLST_Paged(int PageNumber, int PageSize)
		{
			EntityCollection<CategoryEntity> _CategoryCollection = new EntityCollection<CategoryEntity>();
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_CategoryCollection, null, 0, null, null, PageNumber, PageSize);
			}
			return _CategoryCollection;
		}


		
		// Return EntityCollection<CategoryEntity>
		public EntityCollection<CategoryEntity> SelectByCategoryIdLST(int CategoryId)
		{
			EntityCollection<CategoryEntity> _CategoryCollection = new EntityCollection<CategoryEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryId == CategoryId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_CategoryCollection, filter, 0, null);
			}
			return _CategoryCollection;
		}
		
		// Return EntityCollection<CategoryEntity>
		public EntityCollection<CategoryEntity> SelectByCategoryIdLST_Paged(int CategoryId, int PageNumber, int PageSize)
		{
			EntityCollection<CategoryEntity> _CategoryCollection = new EntityCollection<CategoryEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryId == CategoryId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_CategoryCollection, filter, 0, null, null, PageNumber, PageSize);
			}
			return _CategoryCollection;
		}
		
		// Return DataTable
		public DataTable SelectByCategoryIdRDT(int CategoryId)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _CategoryCollection = new EntityCollection(new CategoryEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryId == CategoryId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_CategoryCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return DataTable
		public DataTable SelectByCategoryIdRDT_Paged(int CategoryId, int PageNumber, int PageSize)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _CategoryCollection = new EntityCollection(new CategoryEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryId == CategoryId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_CategoryCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, 0, null, true, null, PageNumber, PageSize);
			}
			return toReturn;
		}


		
		// Return EntityCollection<CategoryEntity>
		public EntityCollection<CategoryEntity> SelectByCategoryNameLST(string CategoryName)
		{
			EntityCollection<CategoryEntity> _CategoryCollection = new EntityCollection<CategoryEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryName == CategoryName);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_CategoryCollection, filter, 0, null);
			}
			return _CategoryCollection;
		}
		
		// Return EntityCollection<CategoryEntity>
		public EntityCollection<CategoryEntity> SelectByCategoryNameLST_Paged(string CategoryName, int PageNumber, int PageSize)
		{
			EntityCollection<CategoryEntity> _CategoryCollection = new EntityCollection<CategoryEntity>();
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryName == CategoryName);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_CategoryCollection, filter, 0, null, null, PageNumber, PageSize);
			}
			return _CategoryCollection;
		}
		
		// Return DataTable
		public DataTable SelectByCategoryNameRDT(string CategoryName)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _CategoryCollection = new EntityCollection(new CategoryEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryName == CategoryName);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_CategoryCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return DataTable
		public DataTable SelectByCategoryNameRDT_Paged(string CategoryName, int PageNumber, int PageSize)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _CategoryCollection = new EntityCollection(new CategoryEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(CategoryFields.CategoryName == CategoryName);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_CategoryCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, 0, null, true, null, PageNumber, PageSize);
			}
			return toReturn;
		}


		
		

	}
}
